package no.apero.wiiamp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Player {
	
	private void exec(String commands) {
		String[] cmd = {FileReader.getInstance().getClampPath(), commands};
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	private String execWithReturn(String commands) {
		String[] cmd = {FileReader.getInstance().getClampPath(), commands};
		String line = null;
		try {
		     //Name of the track
		     Process p = Runtime.getRuntime().exec(cmd);
		     BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
		     line = input.readLine();
		     input.close();
		     }
		    catch (Exception err) {
		     err.printStackTrace();
		     }
		    return line;	
	}
	
	public String add(String song) {
		String cmd = "/PLADD \"" + FileReader.getInstance().getCurrentDir() + "\\" + song + "\" /PLAY";
		System.out.println(cmd);
		exec(cmd);
		return "ok";
	}

	public String play() {
		exec("/PLAY");
		return "ok";
	}

	public String getTitle(){
		String Title = execWithReturn("/TITLE");
		return Title;
	}
	
	public String getTime(){
	     String Time = execWithReturn("/POS"); 
	     return Time;
	}
	public String pause() {
		exec("/PAUSE");
		return "ok";
	}
	public String next() {
		exec("/NEXT");
		return "ok";
	}
	
	public String prev() {
		exec("/PREV");
		return "ok";
	}
	
}
